package com.itheima.reflect.myreflect4;

import java.lang.reflect.Field;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        //method1();
        //mehtod2();
        //method3();
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect4.Student");
        Field money = clazz.getDeclaredField("money");
        System.out.println(money);
    }

    private static void method3() throws ClassNotFoundException, NoSuchFieldException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect4.Student");
        Field name = clazz.getField("name");
//        Field name1 = clazz.getField("name1");
//        Field money = clazz.getField("money");

        System.out.println(name);
    }

    private static void mehtod2() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect4.Student");
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }

    private static void method1() throws ClassNotFoundException {
        // 1. 获取class对象
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect4.Student");
        // 2. 获取 Fields 对象
        Field[] fields = clazz.getFields();
        // 3. 遍历
        for (Field field : fields) {
            System.out.println(field);
        }
    }
}
